package controller

import (
	"blog_server/global"
	"blog_server/internal/model/request"
	"blog_server/internal/service"
	"blog_server/pkg/app"
	"blog_server/pkg/errcode"
	"github.com/gin-gonic/gin"
	"strconv"
)

type ArticleController struct{}

func NewArticleController() ArticleController {
	return ArticleController{}
}

func (a ArticleController) Create(c *gin.Context) {
	param := request.CreateArticleRequest{}
	response := app.NewResponse(c)

	err := c.ShouldBind(&param)
	if err != nil {
		global.Logger.Errorf("ShouldBind errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	svc := service.New(c.Request.Context())
	err = svc.CreateArticle(&param)
	if err != nil {
		global.Logger.Errorf("svc.CreateArticle err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
	return
}

func (a ArticleController) GetArticleByID(c *gin.Context) {
	response := app.NewResponse(c)
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		global.Logger.Errorf("app.BindAndValid errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	svc := service.New(c.Request.Context())
	article, err := svc.GetArticleByID(uint(id))
	if err != nil {
		global.Logger.Errorf("svc.GetArticleByID err: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"article": article})
}

func (a ArticleController) GetArticleByCategoryID(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	res := c.Param("category_id")
	categoryId, err := strconv.Atoi(res)
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	pageSize, _ := app.GetPageSize(c)
	page, _ := app.GetPage(c)
	list, cnt, err := svc.GetArticleByCategoryID(uint(categoryId), page, pageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetArticleByCategoryID errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": list, "cnt": cnt})
}

func (a *ArticleController) GetArticleList(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	pageSize, _ := app.GetPageSize(c)
	page, _ := app.GetPage(c)
	list, cnt, err := svc.GetArticleList(page, pageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetArticleList errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": list, "cnt": cnt})
}

func (a *ArticleController) GetFirstPageArticle(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	pageSize, _ := app.GetPageSize(c)
	page, _ := app.GetPage(c)
	list, err := svc.GetFirstPageArticleList(page, pageSize)
	if err != nil {
		global.Logger.Errorf("svc.GetArticleList errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": list})
}

func (a ArticleController) GetArticleListBySearchText(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	searchText := c.Param("search_text")
	res, err := svc.GetArticleListBySearchText(searchText)
	if err != nil {
		global.Logger.Errorf("svc.GetArticleListBySearchText errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(gin.H{"list": res})
}

func (a ArticleController) UpdateArticle(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	param := request.CreateArticleRequest{}
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		global.Logger.Errorf("strconv.Atoi errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = c.ShouldBind(&param)
	if err != nil {
		global.Logger.Errorf("ShouldBind errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.UpdateArticle(uint(id), param)
	if err != nil {
		global.Logger.Errorf("svc.UpdateArticle errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}

func (a ArticleController) GetEditPgeArticleById(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		global.Logger.Errorf("app.BindAndValid errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	articles, err := svc.GetEditPageArticleById(uint(id))
	if err != nil {
		global.Logger.Errorf("svc.GetEditPageArticleById errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(articles)
}

func (a ArticleController) DeleteArticle(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		global.Logger.Errorf("app.BindAndValid errs: %v", err)
		response.ToErrorResponse(errcode.InvalidParams)
		return
	}
	err = svc.DeleteArticle(uint(id))
	if err != nil {
		global.Logger.Errorf("svc.DeleteArticle errs: %v", err)
		response.ToErrorResponse(errcode.ServerError)
		return
	}
	response.ToSuccessResponse(nil)
}
